#!/usr/bin/env ruby

$:.unshift File.join(File.dirname(__FILE__),'..','lib')

require 'roadrunner'

require 'rubygems'
require 'activerecord'
require 'benchmark'
require 'logger'
require 'pp'

mysql=RoadRunner.new

=begin
  RoadRunner::mode =>
  /thread/,/t/
  /process/,/p/
  else.
  Defined in run.rb
  当mode为进程的时候,RoadRunner将产生最真实的压力
=end

mysql.mode='p'

begin
  ActiveRecord::Base.establish_connection(
    :adapter =>"mysql",
    :username => "houyi",
    :password =>"111111",
    :database => "SNS",
    :host =>"10.250.6.30"
  )
  $log.info "mysql Connect Success."
rescue => e
  $log.error "mysql Connect Failed!"
  $log.error e.to_s
end

class RRData < ActiveRecord::Base
  set_table_name "qa_posts"
  # set_primary_key "ID"
end

mysql.init do
  # users决定同时有多少并发用户一起执行action
  # iterations决定每个用户执行多少次
  mysql.users,mysql.iterations=100,10000
end

SQL=""

mysql.action do
  # ts = Benchmark.measure{
    RRData.find_by_sql(SQL)
  # }
  # mysql.log.error "RRData.find_by_sql #{SQL}   ts => #{ts}"
end

mysql.ended do
end

mysql.run
mysql.report

p "Tps could get : RoadRunner::tps => #{mysql.tps}"
